package map.android.baidu.carpool.utils;

import com.baidu.components.api.extra.APIProxy;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import map.android.baidu.carpool.R;

public class CarpoolOkCancelDialog extends Dialog {
    public static final String LOGTAG = "CarOkCancelDialog";

    private static final double SCALE = 0.85;

    private View mView;
    private TextView mTitleTextView;
    private TextView mInfoTextTextView;
    private Button mOkButton;
    private Button mCancelButton;

    public static CarpoolOkCancelDialog showDialog(Activity activity, int titleId, int infoId, int ok, int cancel,
                                               View.OnClickListener okListener) {
        if (activity == null) {
            return null;
        }
        CarpoolOkCancelDialog taxiOkCancelDialog = new CarpoolOkCancelDialog(activity, titleId, infoId, ok, cancel);
        taxiOkCancelDialog.setOkButtonOnClickListener(okListener);
        taxiOkCancelDialog.show();

        return taxiOkCancelDialog;
    }

    public static CarpoolOkCancelDialog showDialog(Activity activity, String infoId,
                                               View.OnClickListener okListener, View.OnClickListener cancleListener) {
        if (activity == null) {
            return null;
        }
        CarpoolOkCancelDialog taxiOkCancelDialog = new CarpoolOkCancelDialog(activity, "提示", infoId, "取消订单", "保留订单");
        taxiOkCancelDialog.setOkButtonOnClickListener(okListener);
        taxiOkCancelDialog.setCancelButtonOnClickListener(cancleListener);
        taxiOkCancelDialog.show();

        return taxiOkCancelDialog;
    }

    public static CarpoolOkCancelDialog showDialog(Activity activity, String infoId, String title,
                                               View.OnClickListener okListener, View.OnClickListener cancleListener) {
        if (activity == null) {
            return null;
        }
        CarpoolOkCancelDialog taxiOkCancelDialog = new CarpoolOkCancelDialog(activity, title, infoId, "取消", "确定");
        taxiOkCancelDialog.setOkButtonOnClickListener(okListener);
        taxiOkCancelDialog.setCancelButtonOnClickListener(cancleListener);
        taxiOkCancelDialog.show();

        return taxiOkCancelDialog;
    }

    public static CarpoolOkCancelDialog showDialogPay(Activity activity, String infoId, String title,
                                                  View.OnClickListener okListener,
                                                  View.OnClickListener cancleListener) {
        if (activity == null) {
            return null;
        }
        CarpoolOkCancelDialog taxiOkCancelDialog = new CarpoolOkCancelDialog(activity, title, infoId, "现金支付", "不，在线支付");
        taxiOkCancelDialog.setOkButtonOnClickListener(okListener);
        taxiOkCancelDialog.setCancelButtonOnClickListener(cancleListener);
        taxiOkCancelDialog.show();

        return taxiOkCancelDialog;
    }

    public static CarpoolOkCancelDialog showDialog(Activity activity, String titleId, String infoId, String ok,
                                               String cancel, View.OnClickListener okListener) {
        if (activity == null) {
            return null;
        }
        CarpoolOkCancelDialog taxiOkCancelDialog = new CarpoolOkCancelDialog(activity, titleId, infoId, ok, cancel);
        taxiOkCancelDialog.setOkButtonOnClickListener(okListener);
        taxiOkCancelDialog.show();

        return taxiOkCancelDialog;
    }

    public static CarpoolOkCancelDialog showDialog(Activity activity, String titleId, String infoId, String ok,
                                               String cancel, View.OnClickListener okListener
            , View.OnClickListener cancleListener) {
        if (activity == null) {
            return null;
        }
        CarpoolOkCancelDialog taxiOkCancelDialog = new CarpoolOkCancelDialog(activity, titleId, infoId, ok, cancel);
        taxiOkCancelDialog.setOkButtonOnClickListener(okListener);
        taxiOkCancelDialog.setCancelButtonOnClickListener(cancleListener);
        taxiOkCancelDialog.show();

        return taxiOkCancelDialog;
    }

    public CarpoolOkCancelDialog(Activity activity, int titleId, int infoId, int okId, int cancelId) {
        super(activity);
        if (activity == null) {
            return;
        }

        initUI(activity, titleId, infoId, okId, cancelId);
    }

    public CarpoolOkCancelDialog(Activity activity, String title, String info, String ok, String cancel) {
        super(activity);
        if (activity == null) {
            return;
        }
        initUI(activity, title, info, ok, cancel);
    }

    private void initUI(Activity activity, int titleId, int infoId, int okId, int cancelId) {
        String title = APIProxy.res().getString(titleId);
        String info = APIProxy.res().getString(infoId);
        String ok = APIProxy.res().getString(okId);
        String cancel = APIProxy.res().getString(cancelId);

        initUI(activity, title, info, ok, cancel);
    }

    private void initUI(Activity activity, String title, String info, String ok, String cancel) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCancelable(false);

        mView = APIProxy.res().inflate(R.layout.car_ok_cancel_dialog, null);
        setContentView(mView);

        mTitleTextView = (TextView) mView.findViewById(R.id.car_okcancel_dialog_title_textview);
        mInfoTextTextView = (TextView) mView.findViewById(R.id.car_okcancel_dialog_info_textview);
        mCancelButton = (Button) mView.findViewById(R.id.car_okcancel_dialog_cancel_button);
        mCancelButton.setText(cancel);
        mOkButton = (Button) mView.findViewById(R.id.car_okcancel_dialog_ok_button);
        mOkButton.setText(ok);
        mCancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mOkButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        if (title != null && !title.equals("")) {
            mTitleTextView.setText(title);
            mTitleTextView.setVisibility(View.VISIBLE);
        } else {
            mTitleTextView.setVisibility(View.GONE);
        }
        mInfoTextTextView.setText(Html.fromHtml(info));
    }

    public void setInfo(int infoId) {
        String info = APIProxy.res().getString(infoId);
        setInfo(info);
    }

    public void setInfo(String info) {
        if (mInfoTextTextView != null) {
            mInfoTextTextView.setText(info);
        }
    }

    public void setTitle(int titleId) {
        String title = APIProxy.res().getString(titleId);
        setTitle(title);
    }

    public void setTitle(String title) {
        if (mTitleTextView != null) {
            mTitleTextView.setText(title);
        }
    }

    public void setOkButtonOnClickListener(final View.OnClickListener l) {
        if (l == null) {
            return;
        }
        if (mOkButton != null) {
            mOkButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    l.onClick(v);
                    dismiss();
                }
            });
        }
    }

    public void setCancelButtonOnClickListener(final View.OnClickListener l) {
        if (l == null) {
            return;
        }
        if (mCancelButton != null) {
            mCancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    l.onClick(v);
                    dismiss();
                }
            });
        }
    }

    @Override
    public void show() {
        super.show();
        ColorDrawable dw = new ColorDrawable(0x0000ff00);
        getWindow().setBackgroundDrawable(dw);
        int width = APIProxy.sys().getScreenWidth() * 9 / 10;
        int height = -2;
        getWindow().setLayout(width, height);
    }
}